// 后端数据接口的统一管理和方法执行模块
const baseUrl = "https://oa.itany.com/exchange";
const urls = {
    findAllLoop:"/loop/findAllLoop",
    findArticleList:"/articlelist/findArticleList",
    findAllType:"/type/findAllType",
    findDetail: "/articlelist/findAllArticle",
    users: "/users",
    fileUploadUrl:"/fileUpload/uploadImg",
    addProductUrl:"/articlelist/addArticle"

}
const header = {
    "content-type": "application/x-www-form-urlencoded"
}
function initSuccess({ statusCode, data }){
    if(statusCode == 200){
        return data;
    }else{
        return Promise.reject("请求失败");
    }
}
function loadLoopList(success,error){
    wx.request({
        url: baseUrl+usrls.findAllLoop,
        method: 'POST',
        header,
        success: success,
        fail: error
    });
}
function loadLoopList1(){
    return new Promise(function(resolve,reject){
        wx.request({
            url:baseUrl+urls.findAllLoop,
            method:"POST",
            header,
            success:resolve,
            fail:reject
        });
    }).then(initSuccess);
}
function loadProductList(params={page:1,rows:4}){
    return new Promise(function(resolve,reject){
        wx.request({
            url:baseUrl+urls.findArticleList,
            method:"POST",
            data:params,
            header,
            success: resolve,
            fail: reject
        });
    }).then(initSuccess);
}
function loadTypeList(){
    return new Promise(function(resolve,reject){
        wx.request({
            url:baseUrl+urls.findAllType,
            method:"POST",
            header,
            success:resolve,
            fail:reject
        });
    }).then(initSuccess)
}
function loadProductById(params){
    return new Promise(function(resolve,reject){
        // console.log(params);
        if(params&&params.id){
            wx.request({
                url: baseUrl + urls.findDetail,
                method:"POST",
                data:params,
                header,
                success:resolve,
                fail:reject
            })
        }else{
            reject({
                message:"参数错误"
            })
        }
    }).then(initSuccess);
}
function errorFunction(event,success=function(){}){
    wx.showModal({
        title:"数据加载错误",
        content:event.message + ":请联系管理员",
        showCancel:false,
        confirmText:"确定",
        confirmColor:"#007bff",
        success:success
    });
}
function findUserByOpenId(openId) {
    return new Promise(function(resolve,reject) {
        if(openId){
            wx.request({
                url:baseUrl+urls.users+"/"+openId,
                method: 'GET',
                header,
                success: resolve,
                fail: reject,
            });
        }else{
            reject({
                message:"参数错误"
            });
        }
    }).then(initSuccess);
}
function addUser(user) {
    return new Promise(function(resolve, reject ){
        wx.request({
            url: baseUrl+urls.users,
            header,
            data:user,
            method: 'POST',
            success: resolve,
            fail: reject
        });
    }).then(initSuccess)
}
function uploadFile(path) {
    return new Promise(function (resolve, reject) {
        var upTask = wx.uploadFile({
            url: baseUrl+urls.fileUploadUrl,
            filePath: path,
            name: "img",
            success: resolve,
            fail: reject
        });
    })
    .then(initSuccess)
    .then(function(data){
        return JSON.parse(data);
    })
}
function addProduct(params) {
    return new Promise(function (resolve, reject ){
        wx.request({
            url:baseUrl+urls.addProductUrl,
            method: "POST",
            data:params,
            header,
            success: resolve,
            fail: reject
        });
    }).then(initSuccess);
}
module.exports = {
    loadLoopList,
    loadLoopList1,
    loadProductList,
    loadTypeList,
    loadProductById,
    errorFunction,
    findUserByOpenId,
    addUser,
    uploadFile,
    addProduct
}